package com.huahua.api.controller.utility;

import com.huahua.api.entity.base.R;
import com.huahua.api.entity.utility.svg.param.SvgGenerateParam;
import com.huahua.api.entity.utility.svg.vo.SvgTemplateVO;
import com.huahua.api.service.SvgDrawingService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * SVG绘图控制器
 *
 * @author System
 * @date 2025-07-06
 */
@RestController
@RequestMapping("/utility/svg")
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class SvgDrawingController {

    private final SvgDrawingService svgDrawingService;

    /**
     * 获取所有SVG模板类型
     *
     * @return 模板列表
     */
    @GetMapping("/templates")
    public Mono<R<List<SvgTemplateVO>>> getAllTemplates() {
        return svgDrawingService.getAllTemplates()
                .map(R::success)
                .onErrorResume(e -> Mono.just(R.error(e.getMessage())));
    }

    /**
     * 生成SVG图片
     *
     * @param param 生成参数
     * @return 流式返回SVG内容
     */
    @PostMapping(value = "/generate", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateSvg(@RequestBody SvgGenerateParam param) {
        return svgDrawingService.generateSvg(param);
    }
}